//
//  LGSearchMedicineController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/19.
//  搜索药品

#import "LGSearchMedicineController.h"
//上传药物
#import "LGAddCustomMedicineController.h"
#import "LGSearchMedicineCell.h"
@interface LGSearchMedicineController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *addButton;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGSearchMedicineController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"药品名称";
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self.view addSubview:self.addButton];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    self.patientid = (_patientid && _patientid.length>0)?_patientid:@"";
    [RequestUtil withPOST:@"/doctor/Sportsprescription.MedicinePrescription/medicineAll" parameters:@{@"keyword":self.searchTF.text,@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}

//上传药物
-(void)addCustomMedicine{
    LGAddCustomMedicineController *controller = [[LGAddCustomMedicineController alloc]init];
    controller.patientid = self.patientid;
    controller.tempPrescripid = self.tempPrescripid;
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    [self requestData];
//    NSString *lang = textField.textInputMode.primaryLanguage;// 键盘输入模式
//    if ([lang isEqualToString:@"zh-Hans"]){
//        UITextRange *selectedRange = [textField markedTextRange];
//        if (!selectedRange) { // 没有高亮
//            NSLog(@">>???>>>%@",textField.text);
//
//            if(textField.text.length == 0){
//                [self.emptyView stopBufferAction];
////                self.recordTableView.hidden = NO;
////                [self.recordTableView reloadData];
//            }else{
//                [self requestData];
////                self.recordTableView.hidden = YES;
//            }
//        }
//    }else{
//        if(textField.text.length == 0){
//            [self.emptyView stopBufferAction];
////            self.recordTableView.hidden = NO;
////            [self.recordTableView reloadData];8sajdjhgfds
//        }else{
//            [self requestData];
////            self.recordTableView.hidden = YES;
//        }
//    }
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSearchMedicineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    cell.content = LGNSString(self.dataArray[indexPath.row][@"name"]);
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.canSelect == YES){
        if(self.selectMedicineResult){
            self.selectMedicineResult(self.dataArray[indexPath.row]);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    headView.backgroundColor = [UIColor clearColor];
    headView.topMargin = viewPix(10);
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footerView.backgroundColor = [UIColor clearColor];
    footerView.bottomMargin = viewPix(10);
    return footerView;
}


-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        _searchTF.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _searchTF.cornerRidus = 15;
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.returnKeyType = UIReturnKeySearch;
        _searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchTF.placeholder = @"请输入药品名称";
        _searchTF.font = LGFont(16);
        _searchTF.delegate = self;
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = [self leftView];
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTF;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(80), Screen_W, Screen_H-topBarHeight-viewPix(80)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSearchMedicineCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIView *)leftView{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(36), viewPix(54))];
    UIImageView *searchIcon = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(19), viewPix(16), viewPix(16))];
    searchIcon.image = [UIImage imageNamed:@"searchGray16"];
    [leftView addSubview:searchIcon];
    return leftView;
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"找不到药物？" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#FFFFFF" selectColor:@"#FFFFFF"];
        _addButton.frame = CGRectMake(Screen_W-viewPix(117), Screen_H-viewPix(140)-bottomSafeBarHeight/2.0, viewPix(150), viewPix(36));
        _addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _addButton.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(18), 0, 0);
        _addButton.backgroundColor = [UIColor colorWithString:kThemeColor];
        _addButton.cornerRidus = viewPix(18);
        [_addButton addTarget:self action:@selector(addCustomMedicine) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
